﻿using Advantech.Motion;
using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VisionCore
{
    public class ExcHelper: ProcedureHelper_Exc
    {
        public DI_Exc DI { get; set; } = new DI_Exc();
        public DO_Exc DO { get; set; } = new DO_Exc();
        public Ao_Exc Ao { get; set; } = new Ao_Exc();
        public LaserHelper Laser1 { get; set; }

        Thread thread;
        bool isRun = true;

        Motion_Exc motion = new Motion_Exc();
        Status_ExcStatus status = new Status_ExcStatus();
        Status_ExcInit ExcInit = new Status_ExcInit();


        Status_LoadCV1 ExcLD1= new Status_LoadCV1();
        Status_LoadCV2 ExcLD2 = new Status_LoadCV2();
        Status_Insp1 Exc_Insp1 = new Status_Insp1();
        Status_Insp2 Exc_Insp2 = new Status_Insp2();
        Status_Insp3 Exc_Insp3 = new Status_Insp3();
        Status_Insp4 Exc_Insp4 = new Status_Insp4();


        bool firstScan = true;

        public void Start()
        {
            //Laser1 = new LaserHelper(ParameterHelper.SysParam.COM.Laser_IP,50002);
            //Laser1.Start();

            thread = new Thread(ThreadFunc);
            thread.IsBackground = true;
            thread.Start();

            DelayOnHelper_Exc.Start();
        }

        public void Quit()
        {
            isRun  = false;
            DelayOnHelper_Exc.Quit();
        }

        public void ThreadFunc()
        {
            ProcessHelper.SetThreadAffinityMask(ProcessHelper.GetCurrentThread(), new UIntPtr(ProcessHelper.SetCpuID(6)));

            while (isRun)
            {

                SystemTimeCounter T1 = new SystemTimeCounter();
                T1.Start();
                if (Kernel.motion.IsRun)
                {
                    if (firstScan)
                    {
                        Thread.Sleep(1000);
                        PresenceUpdate = true;
                        firstScan = false;
                    }
                    //资料更新
                    if (PresenceUpdate)
                    {
                        ParameterHelper.ExcST = ParameterHelper.ExcSTDAL.Load();
                    

                       
                        PresenceUpdate = false;
                    }
                    status.Main();
                    if (!ManualMode||ExcTR_Exchanging)
                    {
                        if (InitMode)
                        {
                            ExcInit.Main();
                        }
                        ExcLD1.Main();
                        ExcLD2.Main();

                        Exc_Insp1.Main();
                        Exc_Insp2.Main();
                        Exc_Insp3.Main();
                        Exc_Insp4.Main();

                        //ExcST.Main();

                    }
                    else
                    {
                        CV1StatusCode = LDCVStatus.Init;
                        CV2StatusCode = LDCVStatus.Init;
                        Insp1StatusCode = InspStatus.Init;
                        Insp2StatusCode = InspStatus.Init;
                        Insp3StatusCode = InspStatus.Init;
                        Insp4StatusCode = InspStatus.Init;


                        ExcInitStatusCode = ExcInitStatus.Init;
                        ExcSTStatusCode = ExcSTStatus.Init;
                
                 
                    }

                    motion.OP_Auto();
                    motion.DORun();

                    motion.CV1_TrackSW_YRun();
                    motion.CV2_TrackSW_YRun();
                    motion.CV1_YRun(); 
                    motion.CV2_YRun();
                    motion.CCD1_XRun();
                    motion.CCD1_YRun();
                    motion.CCD1_ZRun();
                    motion.CCD2_XRun();
                    motion.CCD2_YRun();
                    motion.CCD2_ZRun();
                    motion.Insp1_YRun();
                    motion.Insp2_YRun(); 
                    motion.Insp3_YRun();
                    motion.Insp4_YRun();
                    motion.EntryCV1_Run();
                    motion.EntryCV2_Run();
                    motion.Insp1_CV_Run();
                    motion.Insp2_CV_Run();
                    motion.Insp3_CV_Run();
                    motion.Insp4_CV_Run();

                    motion.RM_Insp1_ZCtr();
                    motion.RM_Insp2_ZCtr();
                    motion.RM_Insp3_ZCtr();
                    motion.RM_Insp4_ZCtr();
                    if (EQ_Reset)
                    {
                        Warnings.WarningReset();
                        Alarms.AlarmReset();
                    }

                    if (Kernel.motion.CV1_TrackSW_Y.HomeStart || Kernel.motion.CV1_Y.HomeStart || Kernel.motion.CV2_TrackSW_Y.HomeStart || Kernel.motion.CV2_Y.HomeStart || Kernel.motion.Insp1_Y.HomeStart || Kernel.motion.Insp2_Y.HomeStart || Kernel.motion.Insp3_Y.HomeStart || Kernel.motion.Insp4_Y.HomeStart || Kernel.motion.CCD1_X.HomeStart || Kernel.motion.CCD1_Y.HomeStart || Kernel.motion.CCD1_Z.HomeStart || Kernel.motion.CCD2_X.HomeStart || Kernel.motion.CCD2_Y.HomeStart || Kernel.motion.CCD2_Z.HomeStart )
                    {
                        if (ParameterHelper.AixsInitalSetting())
                        {
                            Kernel.motion.F_Home();
                        }
                    }
                }
                double tt = T1.Duration;
                
                SystemTimeCounter.timeBeginPeriod(1);
                Thread.Sleep(1);
                SystemTimeCounter.timeEndPeriod(1);
            }
        }
    }
}
